﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Savage.DotNetLibrary.WebControlLibrary
{
    public class ThumbnailImage : System.Web.UI.WebControls.Image
    {
        public string SourceImagePath
        {
            get { return (string)ViewState["path"]; }
            set { ViewState["path"] = value; }
        }

        public string ThumbnailRootDirectory
        {
            get
            {
                string result = (string) ViewState["dir"];
                if (String.IsNullOrEmpty(result))
                    return "thumbnails";
                else
                    return result;
            }
            set 
            {
                if (value == "thumbnails")
                    ViewState["dir"] = null;
                else
                    ViewState["dir"] = value.TrimEnd('/'); 
            }
        }

        public string ThumbnailFilename
        {
            get { return (string)ViewState["filename"]; }
            set { ViewState["filename"] = value; }
        }

        public bool UseOriginalFilename
        {
            get
            {
                bool? result = (bool?)ViewState["useOriginal"];
                if (!result.HasValue)
                    return true;
                else
                    return result.Value;
            }
            set 
            {
                if (value)
                    ViewState["useOriginal"] = null;
                else
                    ViewState["useOriginal"] = value; }
        }

        public int ThumbnailWidth
        {
            get 
            {
                int? result = (int?)ViewState["width"];
                if (!result.HasValue)
                    return 100;
                else
                    return result.Value;
            }
            set 
            {
                if (value == 100)
                    ViewState["width"] = null;
                else
                    ViewState["width"] = value; 
            }
        }

        public int ThumbnailHeight
        {
            get
            {
                int? result = (int?)ViewState["height"];
                if (!result.HasValue)
                    return 100;
                else
                    return result.Value;
            }
            set
            {
                if (value == 100)
                    ViewState["height"] = null;
                else
                    ViewState["height"] = value;
            }
        }

        public System.Drawing.Drawing2D.InterpolationMode InterpolationMode
        {
            get
            {
                string result =  (string)ViewState["mode"];
                if (String.IsNullOrEmpty(result))
                    return System.Drawing.Drawing2D.InterpolationMode.Default;
                else
                    return (System.Drawing.Drawing2D.InterpolationMode) Enum.Parse(typeof(System.Drawing.Drawing2D.InterpolationMode), result, true);
            }
            set 
            {
                if (value == System.Drawing.Drawing2D.InterpolationMode.Default)
                    ViewState["mode"] = null;
                else
                    ViewState["mode"] = System.Text.RegularExpressions.Regex.Replace(value.ToString(), @"^System\.Drawing\.Drawing2D\.InterpolationMode\.", "");
            }
        }

        public string ThumbnailPath
        {
            get
            {
                return "~/" + ThumbnailRootDirectory + "/" + InterpolationMode.ToString() + "/" + ThumbnailWidth + "x" + ThumbnailHeight + "/";
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!String.IsNullOrEmpty(SourceImagePath))
            {
                if (String.IsNullOrEmpty(ThumbnailFilename))
                {
                    if (UseOriginalFilename)
                        ThumbnailFilename = System.IO.Path.GetFileName(SourceImagePath);
                    else
                        ThumbnailFilename = Guid.NewGuid().ToString().Replace("-", "") + System.IO.Path.GetExtension(SourceImagePath);
                }
                base.ImageUrl = ThumbnailPath + ThumbnailFilename;

                string thumbnailFilePath = System.Web.HttpContext.Current.Server.MapPath(base.ImageUrl);
                if (!System.IO.File.Exists(thumbnailFilePath) && System.IO.File.Exists(SourceImagePath))
                {
                    Savage.DotNetLibrary.ImageLibrary.ImageUtility iu = new Savage.DotNetLibrary.ImageLibrary.ImageUtility(SourceImagePath);
                    iu.CreateThumbnail(thumbnailFilePath, ThumbnailWidth, ThumbnailHeight, InterpolationMode);
                }
            }

            base.Render(writer);
        }
    }
}
